/**
 * 个人资料的修改
 * @author Qasim <15750783791@163.com>
 * @version 2016/9/5
 */
var self = id = pic = nickname = memberInfo = age = gender = company = education  = address = profession = '';

var index = 1;

var files = [];  

document.addEventListener('plusready' , ready ,  false);

function ready(){
	
	self = plus.webview.currentWebview();
	
	var phone = '';
	
	memverInfo = plus.storage.getItem('memberInfo');
	
   	memverInfo = $.parseJSON(memverInfo);
   	
	id = memverInfo.aid;//用户Id
	
	pic = memverInfo.avatar;//头像
	
	nickname = memverInfo.nickname;//昵称
	
	age = memverInfo.age;//年龄
	
	gender = memverInfo.gender;//性别
	
	company = memverInfo.company;//单位
	 
	education = memverInfo.education;//学历
	
	address = memverInfo.address;//地址
	
	profession = memverInfo.profession;//专业
	
	$('.pic').attr('src', getImgUrl(pic));
	
	$('input').each(function(index){
		
		switch (index){
			case 0:
			
				$(this).attr('value' , nickname); break;
				
			case 1:
			
				$(this).attr('value' , age); break;
				
			case 2:
			
				$(this).attr('value' , (gender == 1 ? '男' : '女' )); break;
				
			case 3:
			
				$(this).attr('value' , company); break;
			
			case 4:
			
				$(this).attr('value' , education); break;
				
			case 5:
			
				$(this).attr('value' , address); break;
				
			case 6:
			
				$(this).attr('value' , profession); break;
		}
		
	})
   		
   	
   	//修改用户头像
 	$('.pic').on('click' , function(){
 		
 		plus.nativeUI.actionSheet( {cancel:"取消",buttons:[{title:"拍照添加"},{title:"相册添加"}]}, function(e){
 			
			if(e.index==1){
				
				var cmr = plus.camera.getCamera();
				
				cmr.captureImage(function(path) {
					
					$('.pic').attr('src','file://'+plus.io.convertLocalFileSystemURL(path));
					
					appendPic(path);
					
				}, function(err) {});
				
			}else if(e.index==2){
				
				plus.gallery.pick(function(path){
					
					$('.pic').attr('src',path);
					
					appendPic(path);
					
			    });
			    
			}
			
		} );
 	})
}


function saveInfo(){
	
	$('input').each(function(index){
   			
   			switch (index){
   				
   				case 0:
   					
   					nickname = $(this).val(); break;
   					
   				case 1:
   				
   					age = $(this).val(); break;
   					
   				case 2:
   				
   					gender = $(this).val() == '男' ? 1 : 0 ; break;
   					
   				case 3:
   				
   					company = $(this).val(); break;
   				
   				case 4:
   				
   					education = $(this).val(); break;
   					
   				case 5:
   				
   					address = $(this).val(); break;
   					 
   				case 6:
   				
   					profession = $(this).val(); break;
   			}
   			
   	}) 
   	
//	console.log(apiRoot + '?m=Home&c=Member&a=updateInfo&aid='+id+'&avatar=' +pic+ '&nickname=' + nickname + '&age=' +age + '&gender=' + gender +'&company=' +company +'&education='  +education+'&address=' +address +'&profession=' + profession);
	
   	plus.nativeUI.showWaiting('更新中...'); 
   	
   	$.ajax({
			
		url : apiRoot + '/Home/Member/updateInfo/' , 
		
		type : 'post' , 
		
		data : {
			
			aid : id , 
			
			avatar : pic , 
			
			nickname : nickname ,
			
			age : age , 
			
			gender : gender , 
			
			company : company , 
			
			education : education , 
			
			address : address ,
			
			profession : profession 
			
		} , 
		
		success : function(data){
			
			plus.nativeUI.closeWaiting();
			
			if(data){
				
				toast('用户数据已更新');
				
				//保存用户数据
				plus.storage.setItem('memberInfo' , data + '')
				
				mui.fire(plus.webview.getWebviewById(self.self_window) , 'updateInfo' ,{
						
					'pic' : pic , 
					
					'nickname' : nickname
					 
				})
				mui.back();
			}
			
		} , 
		error : function(e){
			
			console.log(JSON.stringify(e))
			
			plus.nativeUI.closeWaiting();
			
			toast('连接超时 ，请检查网络');
			
		}
	})
}

// 添加照片
function appendPic(p) {  
	
	var newPath = p.replace(/\./g , new Date().getTime()+'.');
	
	plus.zip.compressImage({
		
			src:p,
			
			dst:newPath,
			
			quality:20
			
		},
		
		function() {
			
			files.push({name:"uploadkey"+index,path:p});
			
			index++;
			
			upload();
			
		},function(error) {
			
			toast('压缩图片失败');
			
	});
}

function upload(){
	
	plus.nativeUI.showWaiting('图片处理中...');
	
	if(files.length <= 0){  
		
		toast("请上传头像！");
		
		return;
		
	}
	
	var server = apiRoot+'/Home/Member/uploadPic/'; 
	
	var task = plus.uploader.createUpload(server,
		
		{method:"POST"},
		
		function(t,status){ //上传完成
			
			plus.nativeUI.closeWaiting();
			
			if(status == 200){
				
				toast('处理完成');
				
				pic = t.responseText;
				
				console.log(pic);
				
				
			}else{
				
				plus.nativeUI.alert("上传失败："+status);
				
				wt.close();
				
			}
		} 
	);
	
	for(var i = 0;i < files.length ; i++){
		
		var f = files[i];
		
		task.addFile(f.path,{key:f.name});
		
	} 
	
	task.start();
}

